package com.example.springcloud.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springcloud.domain.News;
import com.example.springcloud.domain.UserScenicFavorite;
import com.example.springcloud.mapper.UserRouteFavoriteMapper;
import com.example.springcloud.mapper.UserScenicFavoriteMapper;
import com.example.springcloud.request.UserCancelRouteFavoriteRequest;
import com.example.springcloud.request.UserRouteFavoriteRequest;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.example.springcloud.domain.UserRouteFavorite;
import com.example.springcloud.service.IUserRouteFavoriteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户路线收藏Controller
 * 
 * @author helian
 * @date 2025-03-09
 */
@RestController
@RequestMapping("/jiu-mount-huangshan/userRouteFavorite")
@Api(tags = "用户路线收藏管理")
public class UserRouteFavoriteController extends BaseController
{
    @Autowired
    private IUserRouteFavoriteService userRouteFavoriteService;

    @Autowired
    private UserRouteFavoriteMapper userRouteFavoriteMapper;

    /**
     * app查询用户路线收藏列表
     */
    @GetMapping("/list/app")
    @ApiOperation(value = "用户路线收藏列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer")})
    @ApiResponses(@ApiResponse(code = 200, message = "查询用户路线收藏列表成功", response = UserRouteFavorite.class , responseContainer = "List"))
    public TableDataInfo list(Integer userId)
    {
        startPage();
        UserRouteFavorite userRouteFavorite = new UserRouteFavorite();
        userRouteFavorite.setUserId(Long.valueOf(userId));
        List<UserRouteFavorite> list = userRouteFavoriteService.selectUserRouteFavoriteList(userRouteFavorite);
        return getDataTable(list);
    }

    /**
     * 查询用户路线收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserRouteFavorite userRouteFavorite)
    {
        startPage();
        List<UserRouteFavorite> list = userRouteFavoriteService.selectUserRouteFavoriteList(userRouteFavorite);
        return getDataTable(list);
    }

    /**
     * 用户收藏路线
     */
    @PostMapping("/add")
    @ApiOperation(value = "用户收藏路线")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "routeId", value = "路线id", required = true, dataType = "Integer")})
    @ApiResponses(@ApiResponse(code = 200, message = "用户收藏路线成功", response = UserRouteFavorite.class))
    public AjaxResult add(@RequestBody UserRouteFavoriteRequest userRouteFavoriteRequest)
    {
        // 用户已经收藏过该路线，不能重复收藏
        if (userRouteFavoriteMapper.selectUserRouteFavoriteByUserIdAndRouteId(userRouteFavoriteRequest.getUserId(), userRouteFavoriteRequest.getRouteId()) != null) {
            return AjaxResult.error("该路线已收藏");
        }
        UserRouteFavorite userRouteFavorite = new UserRouteFavorite();
        userRouteFavorite.setUserId(userRouteFavoriteRequest.getUserId());
        userRouteFavorite.setRouteId(userRouteFavoriteRequest.getRouteId());
        return toAjax(userRouteFavoriteService.insertUserRouteFavorite(userRouteFavorite));
    }

    /**
     * 用户取消收藏路线
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "用户取消收藏路线")
    @ApiResponses(@ApiResponse(code = 200, message = "用户取消收藏路线成功", response = UserScenicFavorite.class))
    public AjaxResult delete(@RequestBody UserCancelRouteFavoriteRequest userCancelRouteFavoriteRequest) {
        return toAjax(userRouteFavoriteService.deleteUserRouteFavoriteByUserIdAndRouteId(userCancelRouteFavoriteRequest.getUserId(), userCancelRouteFavoriteRequest.getRouteId()));
    }


    /**
     * 导出用户路线收藏列表
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userRouteFavorite:export')")
    @Log(title = "用户路线收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserRouteFavorite userRouteFavorite)
    {
        List<UserRouteFavorite> list = userRouteFavoriteService.selectUserRouteFavoriteList(userRouteFavorite);
        ExcelUtil<UserRouteFavorite> util = new ExcelUtil<UserRouteFavorite>(UserRouteFavorite.class);
        util.exportExcel(response, list, "用户路线收藏数据");
    }

    /**
     * 获取用户路线收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userRouteFavorite:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userRouteFavoriteService.selectUserRouteFavoriteById(id));
    }

    /**
     * 新增用户路线收藏
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userRouteFavorite:add')")
    @Log(title = "用户路线收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserRouteFavorite userRouteFavorite)
    {
        return toAjax(userRouteFavoriteService.insertUserRouteFavorite(userRouteFavorite));
    }

    /**
     * 修改用户路线收藏
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userRouteFavorite:edit')")
    @Log(title = "用户路线收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserRouteFavorite userRouteFavorite)
    {
        return toAjax(userRouteFavoriteService.updateUserRouteFavorite(userRouteFavorite));
    }

    /**
     * 删除用户路线收藏
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userRouteFavorite:remove')")
    @Log(title = "用户路线收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userRouteFavoriteService.deleteUserRouteFavoriteByIds(ids));
    }
}
